<template>
  <el-dialog
    class="corp-set-org-comp"
    title="新增机构"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(800)"
    :before-close="handleCancel">
    <div class="add-org-form-container">
      <el-form
        :model="ruleForm"
        :rules="rules"
        size="medium"
        ref="ruleForm"
        :label-width="$setRem(120)"
        class="rule-form"
      >
        <el-row v-if="parentsData">
          <el-col :span="12">
            <el-form-item label="上级机构名称">
              <el-input
                placeholder="上级机构名称"
                disabled
                :value="parentsData.orgName">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级机构简称">
              <el-input
                placeholder="上级机构简称"
                disabled
                :value="parentsData.orgAbbr">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="orgName" required>
              <el-input
                placeholder="请输入机构名称"
                clearable
                v-model="ruleForm.orgName">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构简称" prop="orgAbbr">
              <el-input
                placeholder="请输入机构简称"
                clearable
                v-model="ruleForm.orgAbbr">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="机构排序" prop="sort">
              <el-input-number
                clearable
                placeholder="请输入机构名称"
                :controls="false"
                v-model="ruleForm.sort">
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="机构简介" prop="orgIntro">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入机构简介"
              clearable
              v-model="ruleForm.orgIntro">
            </el-input>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleCancel">取 消</el-button>
      <el-button
        class="success"
        type="primary"
        :loading="loading"
        @click="hanldeSave">
        保 存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'addOrgComp',
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: true
    },
    options: {
      type: Object,
      default: () => ({})
    },
    // 新增、编辑
    type: {
      type: String,
      default: 'add'
    },
    parentsData: {
      type: Object,
      default: null
    },
    // 当前节点是否为组织（否则为部门）
    hasOrg: {
      type: Boolean,
      default: true
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      ruleForm: {
        id: '',
        orgName: '',
        orgAbbr: '',
        sort: '',
        orgIntro: '',
      },
      // 表单校验规则
      rules: {
        orgName: [
          { required: true, message: '请输入机构名称' },
        ],
        // orgAbbr: [
        //   { required: true, message: '请输入机构简称' },
        // ]
      },
    };
  },
  computed: {
    // 是否展示上级组织选项
    hasShowOrg() {
      return !(this.hasOrg && this.type !== 'add') && this.currentKey;
    }
  },
  watch: {
    // 监听回填数据
    options: {
      deep: true,
      handler: function (data) {
        if (data) {
          const keys = Object.keys(this.ruleForm);
          Object.keys(data).length && keys.forEach(key => {
            this.ruleForm[key] = data[key] || '';
          });
        }
      }
    },
    visible(value) {
      if (!value) {
        this.ruleForm = {
          id: '',
          orgName: '',
          orgAbbr: '',
          sort: '',
          orgIntro: '',
        };
      }
    }
  },
  methods: {
    // 保存
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', {
            ...this.options,
            ...this.ruleForm
          });
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit('cancel');
    }
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/dialog';

  .corp-set-org-comp {
    .add-org-form-container {
      padding-right: 50px;
    }
  }
</style>
